package com.tianyao.compose.baseuse.activity.animate

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.animation.expandHorizontally
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkHorizontally
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.sizeIn
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.tianyao.compose.baseuse.R
import com.tianyao.compose.baseuse.common.logd
import com.tianyao.compose.baseuse.theme.ComposeProTheme
import kotlin.concurrent.timer

/**
 * 为各个组件添加动画
 */
@OptIn(ExperimentalAnimationApi::class)
class AnimatedModifierActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeProTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background
                ) {
                    Greeting()
                }
            }
        }
    }


    @Composable
    fun Greeting() {
        //动画的监听
        AnimatedModifier()
    }


    @Composable
    fun AnimatedModifier() {
        var visible by remember { mutableStateOf(true) }

        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            // 第一个按钮，显示动画
            Button(onClick = {
                visible = true
            }) {
                Text("Show Animation", fontSize = 22.sp)
            }

            // 第二个按钮，隐藏动画
            Button(onClick = {
                visible = false
            }) {
                Text("Hide Animation", fontSize = 22.sp)

            }

            AnimatedVisibility(
                visible = visible,
                enter = fadeIn(),    //外层动画淡入淡出
                exit = fadeOut()
            ) {
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .background(Color.DarkGray)
                ) {
                    Box(
                        modifier = Modifier
                            .align(Alignment.Center)
                            .animateEnterExit(
                                enter = slideInVertically(),    //内层组件滑入滑动屏幕
                                exit = slideOutVertically()
                            )
                            .sizeIn(minWidth = 256.dp, minHeight = 256.dp)
                    ) {
                        Image(
                            painter = painterResource(id = R.mipmap.rabit2),
                            contentDescription = null
                        )
                    }
                }
            }
        }
    }

}
